/**
 * @file
 * @author
 */
var tools = {
  /**
   * 加载层
   * title 参数 显示的标题
   */
  show: function show(title) {
    var self = this;
    var oDiv = document.getElementById('loading');
    oDiv.setAttribute('style', 'display: block;');
    var oTitle = document.getElementById('loading_title');
    oTitle.innerText = title;
    document.getElementById('loadingDelete').addEventListener('click', function () {
      self.hide();
    });
  },
  /**
   * 关闭加载层
   */
  hide: function hide() {
    try {
      var oDiv = document.getElementById('loading');
      oDiv.setAttribute('style', 'display: none;');
    } catch (e) {}
  },
  /**
   * 显示提示信息,
   *  title 参数 显示的标题, time 时间
   */
  msgShow: function (title, time) {
    var time = time || 3000
    var oDiv = document.getElementById('msg');
    oDiv.setAttribute('style', 'display: block;');
    var oTitle = document.getElementById('msg_title');
    oTitle.innerText = title;
    setTimeout(function () {
      var oDiv = document.getElementById('msg');
      oDiv.setAttribute('style', 'display: none;');
    }, time);
  },
  /**
   * 关闭提示信息,
   */
  msgHide: function () {
    try {
      var oDiv = document.getElementById('msg');
      oDiv.setAttribute('style', 'display: none;');
    } catch (e) {
    }
  },
  creatTouchstartEventAndDispatch: function (el) {
    var event = document.createEvent('Events');
    event.initEvent('click', true, true);
    el.dispatchEvent(event);
  },
  getRoundedCanvas: function (sourceCanvas) { // 截取得图
    var canvas = document.createElement('canvas');
    var context = canvas.getContext('2d');
    var width = sourceCanvas.width;
    var height = sourceCanvas.height;
    canvas.width = width;
    canvas.height = height;
    context.beginPath();
    context.arc(width / 2, height / 2, Math.min(width, height) / 2, 0, 2 * Math.PI);
    context.strokeStyle = 'rgba(0,0,0,0)';
    context.stroke();
    context.clip();
    context.drawImage(sourceCanvas, 0, 0, width, height);
    return canvas;
  },
  /**
   * 上传附件,
   *  source 参数 源目标,
   *  iSphoto 是否拍照功能, 默认没有 false 没有
   * name 参数 key
   */
  uploadFile: function (source,iSphoto, cb) {
    var isphoto = iSphoto || false
    document.querySelector('#' + source) && document.querySelector('#' + source).remove();
    var self = this;
    var $input = document.createElement('input');
    $input.style.display = 'none';
    $input.setAttribute('id', source);
    $input.setAttribute('name', 'file');
    $input.setAttribute('type', 'file');
    $input.setAttribute('accept', 'image/*');
//        $input.setAttribute('accept', 'image/*');
    document.body.appendChild($input);
    $input.addEventListener('change', function () {
      var file = $input.files[0];
      $input = null;
      if (cb) {
        cb(file);
      };
      // if (file) {
      //   var reader = new FileReader();
      //   reader.onload = function (event) {
      //     cb(event.target.result);
      //     // var $img = document.createElement('img');
      //     // $img.setAttribute('src', event.target.result);
      //     // document.querySelector(source + ' img')&&document.querySelector(source + ' img').remove();
      //     // document.querySelector(source).append($img);
      //   };
      // };
      // reader.readAsDataURL(file);
    });
    setTimeout(function () {
      var evt = document.createEvent("MouseEvents");
      evt.initEvent("click", false, false);// 或用initMouseEvent()，不过需要更多参数
      $input.dispatchEvent(evt);
    }, 100);
  },
  /**
   * 生产图片,
   * id 绑定元素
   * value 图片流
   */
 createImage : function (source, file)
  {
    if (file) {
      var reader = new FileReader();
      reader.onload = function (event) {
        var $img = document.createElement('img');
        $img.setAttribute('src', event.target.result);
        document.querySelector(source + ' img')&&document.querySelector(source + ' img').remove();
        document.querySelector(source).appendChild($img);
      };
    };
    reader.readAsDataURL(file);
  },
  /**
   * 截取URL参数,
   * name 参数 key
   */
  getQueryString: function (name)
  {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
    var param = window.location.hash.split('?')[1] || '';
    var r = param.match(reg);  //获取url中"?"符后的字符串并正则匹配
    var context = "";
    if (r != null)
      context = r[2];
    reg = null;
    r = null;
    return context == null || context == "" || context == "undefined" ? "" : context;
  },
  /**
   * 时间格式转换,
   * timer 参数 时间戳,
   * @return 2012-12-11 22:11:11 格式
   */
  tranferTime: function (timer) {
    var now = new Date(timer)
    var year=now.getFullYear();
    var month=now.getMonth()+1;
    var date=now.getDate();
    var hour=now.getHours();
    var minute=now.getMinutes();
    var second=now.getSeconds();
    return year+"-"+month+"-"+date+" "+hour+":"+minute+":"+second;
  },

  setTime: function (timer) {
    var now = new Date(timer)
    return window.parseInt(now.getTime());
  },
  /**
   * 时间格式转换,
   * timer 参数 时间戳,
   * @return 2012-12-11 格式
   */
  formatDate : function(objDate){
    var date = (objDate instanceof Date) ? objDate : new Date(objDate);
    // format=format+"";
    // var result="";
    // var i_format=0;
    // var c="";
    // var token="";
    var yyyy=date.getFullYear();
    var m=date.getMonth()+1;
    var d=date.getDate();
    // var w=date.getDay();
    // var h=date.getHours();
    // var n=date.getMinutes();
    // var s=date.getSeconds();
    // var sss=date.getMilliseconds();
    return yyyy + '-' + this.appendZero(m) +'-' + this.appendZero(d);
  },
  appendZero : function(x){
    return(x<0||x>9?"":"0")+x;
  },
  dateFn: function (Str) {
    Str = Str.replace(/(.{4})/,"$1-");
    Str = Str.replace(/(.{7})/,"$1-");
    return Str;
  },
  getHostFn:function () {
    var pos=window.document.location.href.indexOf(window.document.location.pathname);
    return window.document.location.href.substring(0,pos)+ '/';
  }
};
export {tools};
